﻿using System.Runtime.InteropServices;

namespace TrackSystem.IO.Utils
{
    [StructLayoutAttribute(LayoutKind.Sequential)]
    public struct Stream_Info
    {
        public UInt32 u32Channel;   //通道号
        public UInt32 blFlag;       //为1时连接主码流，为0时连接次码流
        public UInt32 u32Mode;      //网络连接模式
        public byte u8Type;         //流数据类型、视频、音频、其他数据
    }

    [StructLayoutAttribute(LayoutKind.Sequential)]
    public struct Stream_Info_Ex
    {
        public UInt32 u32Channel;   //通道号
        public UInt32 u32Stram;     //为0时连接主码流，为1时连接次码流
        public UInt32 u32Mode;      //网络连接模式
        public byte u8Type;         //流数据类型、视频、音频、其他数据
    }


    public enum FILE_FORMAT_E : Int32
    {
        FILE_FORMAT_YUV = 0,              /*YUV*/
        FILE_FORMAT_NUDE_STREAM,          /*stream*/
        FILE_FORMAT_ASF,                  /*ASF*/
        FILE_FORMAT_MP4,                  /*MP4*/
        FILE_FORMAT_AVI,                  /*AVI*/
        FILE_FORMAT_RAW,
        FILE_FORMAT_BUTT
    };

    public enum MEDIA_TYPE_E : Int32
    {
        MEDIA_VIDEO_AUDIO = 0,            /*video and audio*/
        MEDIA_VIDEO,                      /*video*/
        MEDIA_AUDIO,                      /*audio*/
        MEDIA_ALARM,                      /*alarm*/
        MEDIA_SYSTERM,
        MEDIA_BUTT
    }


    /// <summary>
    /// PTZ相机使用类
    /// </summary>
    public class PTZWrapper
    {
        /// <summary>
        /// SDK初始化
        /// </summary>
        /// <returns></returns>
        [DllImport("HISDK.dll", CallingConvention = CallingConvention.StdCall, EntryPoint = "HI_SDK_Init")]
        //[DllImport("HISDK.dll")]
        public static extern Int32 HI_SDK_Init();

        /// <summary>
        /// SDK清除
        /// </summary>
        /// <returns></returns>

        [DllImport("HISDK.dll",CallingConvention = CallingConvention.StdCall, EntryPoint = "HI_SDK_Cleanup")]
        //[DllImport("HISDK.dll")]
        public static extern Int32 HI_SDK_Cleanup();


        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="psHost"></param>
        /// <param name="psUsername"></param>
        /// <param name="psPassword"></param>
        /// <param name="uPort"></param>
        /// <param name="ps32Err"></param>
        /// <returns></returns>
        [DllImport("HISDK.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall, EntryPoint = "HI_SDK_Login")]
        public static extern Int32 HI_SDK_Login(string psHost, string psUsername, string psPassword, short uPort, ref int ps32Err);


        /// <summary>
        /// 用户注册，可以设置超时时间
        /// </summary>
        /// <param name="psHost"></param>
        /// <param name="psUsername"></param>
        /// <param name="psPassword"></param>
        /// <param name="uPort"></param>
        /// <param name="u32TimeOut"></param>
        /// <param name="ps32Err"></param>
        /// <returns></returns>
        [DllImport("HISDK.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall, EntryPoint = "HI_SDK_LoginExt")]
        public static extern Int32 HI_SDK_LoginExt(string psHost, string psUsername, string psPassword, UInt16 uPort, UInt32 u32TimeOut, ref int ps32Err);

        /// <summary>
        /// 用户注销
        /// </summary>
        /// <param name="lHandle"></param>
        /// <returns></returns>
        [DllImport("HISDK.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall, EntryPoint = "HI_SDK_Logout")]
        public static extern Int32 HI_SDK_Logout(Int32 lHandle);

        /// <summary>
        /// 云台控制，
        /// </summary>
        /// <param name="lHandle"></param>
        /// <param name="u32Command">0x3000 停止 0x3001 上  0x3002 下 0x3003 左 0x3004 右 0x3005 焦距+ 0x3006 焦距- 
        ///                          0x3007 焦点前  0x3008 焦点后  0x3009 光圈小 0x3010 光圈大 </param>
        /// <param name="u32Speed"> 0x3F 最大速度  0x00 最小速度</param>
        /// <returns></returns>
        [DllImport("HISDK.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall, EntryPoint = "HI_SDK_PTZControl")]
        public static extern Int32 HI_SDK_PTZControl(Int32 lHandle, uint u32Command, uint u32Speed);


        /// <summary>
        /// 云台控制，单步执行
        /// </summary>
        /// <param name="lHandle"></param>
        /// <param name="u32Command">0x3000 停止 0x3001 上  0x3002 下 0x3003 左 0x3004 右 0x3005 焦距+ 0x3006 焦距- 
        ///                          0x3007 焦点前  0x3008 焦点后  0x3009 光圈小 0x3010 光圈大 </param>
        /// <returns></returns>
        [DllImport("HISDK.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall, EntryPoint = "HI_SDK_PTZControlEx")]
        public static extern Int32 HI_SDK_PTZControlEx(Int32 lHandle, uint u32Command);

        /// <summary>
        /// 视频实时预览
        /// </summary>
        /// <param name="lHandle"></param>
        /// <param name="pWnd"></param>
        /// <param name="streamInfo"></param>
        /// <returns></returns>
        [DllImport("HISDK.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall, EntryPoint = "HI_SDK_RealPlay")]
        public static extern Int32 HI_SDK_RealPlay(Int32 lHandle, IntPtr pWnd, ref Stream_Info streamInfo);

        /// <summary>
        /// 视频停止预览
        /// </summary>
        /// <param name="lHandle"></param>
        /// <returns></returns>
        [DllImport("HISDK.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall, EntryPoint = "HI_SDK_StopRealPlay")]
        public static extern Int32 HI_SDK_StopRealPlay(Int32 lHandle);

        /// <summary>
        /// 视频实时预览
        /// </summary>
        /// <param name="lHandle"></param>
        /// <param name="pWnd"></param>
        /// <param name="streamInfo"></param>
        /// <returns></returns>
        [DllImport("HISDK.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall, EntryPoint = "HI_SDK_RealPlayExt")]
        public static extern Int32 HI_SDK_RealPlayExt(Int32 lHandle, IntPtr pWnd, ref Stream_Info_Ex streamInfoEx);

        /// <summary>
        /// 开始录像
        /// </summary>
        /// <param name="lHandle">操作句柄</param>
        /// <param name="pFilePath">录像文件路径</param>
        /// <param name="eFileFormat">文件格式</param>
        /// <param name="eFlag">录像形式</param>
        /// <param name="s32FileTime">录像时间长度，0为无限制</param>
        /// <returns></returns>
        [DllImport("HISDK.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall, EntryPoint = "HI_SDK_StartRecord")]
        public static extern Int32 HI_SDK_StartRecord(Int32 lHandle, string pFilePath, FILE_FORMAT_E eFileFormat, MEDIA_TYPE_E eFlag, int s32FileTime);

        /// <summary>
        /// 停止录像
        /// </summary>
        /// <param name="lHandle"></param>
        /// <returns></returns>
        [DllImport("HISDK.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall, EntryPoint = "HI_SDK_StopRecord")]
        public static extern Int32 HI_SDK_StopRecord(Int32 lHandle);


        /// <summary>
        /// 抓拍JPEG图像
        /// </summary>
        /// <param name="lHandle"></param>
        /// <param name="sFilePath"></param>
        /// <returns></returns>
        [DllImport("HISDK.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall, EntryPoint = "HI_SDK_CaptureJPEGPicture")]
        public static extern Int32 HI_SDK_CaptureJPEGPicture(Int32 lHandle, string sFilePath);
    }
}
